package basketMatcher;

import java.util.Vector;

import ClassPack.ClassDiagram;
import ClassPack.Class;

public class BasketCreater {

	
	public static Vector<Basket> createBasketVector
	(Vector<ClassDiagram> class_diagrams, 
			Vector<Basket> built_baskets)
		{
		Vector<Vector<Class>> baskets_as_vector =
			createBasketVectorRec (class_diagrams , new Vector<Vector<Class>> ());
		
		Vector<Basket> baskets= new Vector<Basket>();
		
		for ( Vector<Class> v : baskets_as_vector) { 
			Basket b = new Basket(v);
			baskets.add(b);
		}
		
		return baskets;
		
	}
	
	public static Vector<Vector<Class>> createBasketVectorRec
		(Vector<ClassDiagram> class_diagrams 
		, Vector<Vector<Class>> built_baskets  
		) {
			
			if ( class_diagrams.isEmpty() ) { 
			// tail case :
				return built_baskets;
				
			}
			if ( built_baskets.isEmpty() ) {
			// first call :
				ClassDiagram cd = class_diagrams.firstElement();
				class_diagrams.remove(class_diagrams.firstElement());
				
				
				for ( Class class_in_cd : cd.getM_classes()){
					Vector<Class > v = new Vector<Class>();
					v.add(class_in_cd);
					built_baskets.add(v);
				}
				return createBasketVectorRec
				(class_diagrams, built_baskets);
			}
			else  {
			
				ClassDiagram cd = class_diagrams.firstElement();
				class_diagrams.remove(class_diagrams.firstElement());
				
				
				Vector<Vector<Class>>  new_built_baskets = 
					new Vector<Vector<Class>>();
			
			
				for ( Vector<Class > vec_class : built_baskets){
				
				for ( Class class_in_cd : cd.getM_classes()){
					
					
						Vector<Class >vec_class_copy 
							= new Vector<Class>(vec_class);
						vec_class_copy.add(class_in_cd);
						new_built_baskets.add(vec_class_copy);
					}
				}
				return	createBasketVectorRec
				(class_diagrams , new_built_baskets );
				
			}
			
			
		
	}
	
	public static void main(String[] args ) {
		
		Class a1 = new Class();
		a1.setM_class_name("a1");
		
		Class b1 = new Class();
		b1.setM_class_name("b1");
			
		Class c1 = new Class();
		c1.setM_class_name("c1");
		
		a1.addReferencedClass(b1); a1.addReferencedClass(c1);
		b1.addReferencedClass(a1); b1.addReferencedClass(c1);
		c1.addReferencedClass(a1); c1.addReferencedClass(b1);
	
	
		ClassDiagram cd1 = new ClassDiagram();
		cd1.setName("cd1");
		
		cd1.addClass(a1);
		cd1.addClass(b1);
		cd1.addClass(c1);
		
		Class a2 = new Class();
		a2.setM_class_name("a2");
		
		Class b2 = new Class();
		b2.setM_class_name("b2");
			
		Class c2 = new Class();
		c2.setM_class_name("c2");
		
		a2.addReferencedClass(b2); a2.addReferencedClass(c2);
		b2.addReferencedClass(a2); b2.addReferencedClass(c2);
		c2.addReferencedClass(a2); c2.addReferencedClass(b2);
		
		ClassDiagram cd2 = new ClassDiagram();
		cd2.setName("cd2");
		
		cd2.addClass(a2);
		cd2.addClass(b2);
		cd2.addClass(c2);
		
		
		Class a3 = new Class();
		a3.setM_class_name("a3");
		
		Class b3 = new Class();
		b3.setM_class_name("b3");
			
		Class c3 = new Class();
		c3.setM_class_name("c3");
		
		a3.addReferencedClass(b3); a3.addReferencedClass(c3);
		b3.addReferencedClass(a3); b3.addReferencedClass(c3);
		c3.addReferencedClass(a3); c3.addReferencedClass(b3);
		
		ClassDiagram cd3 = new ClassDiagram();
		cd3.setName("cd3");
		
		cd3.addClass(a3);
		cd3.addClass(b3);
		cd3.addClass(c3);
		
	
		Vector<ClassDiagram> cd_vec = new Vector<ClassDiagram>();
		cd_vec.add(cd1);
		cd_vec.add(cd2);
		cd_vec.add(cd3);
	
		
		System.out.println(BasketCreater.
				createBasketVectorRec(cd_vec, new Vector<Vector<Class>>()));
	}
}
